import {fetchLogin,fetchUserInfo} from "@/api/user.js"
import {fetchCates} from "@/api/good.js"
export function switchSize(payload) {
  return { type: "APP_SIZE", payload }
}
export function switchLang(payload) {
  return { type: "APP_LANG", payload }
}
export function switchColor(payload){
  return { type:"APP_COLOR",payload}
}
export function login(data){
  return function (dispatch){
    fetchLogin(data).then(({token})=>{
      if(token){
      // console.log("服务器响应",res);
      localStorage.setItem("token",token)
      dispatch({type:"USER_LOGIN",payload:token})}
    })
  }
}

export function getInfo(){
  return function (dispatch){
    fetchUserInfo().then((res)=>{
      if(res&&res.roles){
      dispatch({type:"USER_INFO",payload:res})
    }})
  }
}

export function resetUser(color){
  return {type:"USER_RESET",payload:null}
}

export function getCates(){
  return dispatch =>{
    fetchCates().then(res=>{
      if(res.list){
        dispatch({type:'GOOD_CATES',payload:res.list})
      }
    })
  }
}